package com.daihou.utils;

import com.daihou.config.TheAuth;
import com.daihou.model.DHInterfaceAuth;
import com.daihou.model.ceres.EnvironmentEnum;

import java.util.*;
import java.util.stream.Collectors;

public class EnvironmentUtil {
    private static ResourceBundle bundle = ResourceBundle.getBundle("environment", Locale.CHINA);
    public static String getUrl(EnvironmentEnum name) {
        return bundle.getString(name.toString());
    }

    public static Map<String, String> getHeaders() {
        Map<String, String> headers = new HashMap<>();
        try {
            //设置请求头中的Authorization
            headers.put("Authorization", TheAuth.getAuth());
            //需要admin权限
            headers.put("X-UserName", "admin");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return headers;
    }
    public static String getUrl(DHInterfaceAuth name) {
        String address = bundle.getString("DaiHou_URL");
        String uri = "";
        String testUrl;
        if (name == DHInterfaceAuth.AUTH) {
            uri = bundle.getString("auth.uri");
        }
        testUrl = address + uri;
        return testUrl;
    }

    public static String getUrl(String ipAdress, String configName, String testUrlName) {
        ResourceBundle bundle = ResourceBundle.getBundle(configName, Locale.CHINA);
//        String address = bundle.getString("DaiHou_URL");
        String address = bundle.getString(ipAdress);
        String testUrl;
        String preUrlName = testUrlName + ".uri";
        List<String> keys = bundle.keySet().stream().filter(s -> s.equalsIgnoreCase(preUrlName)).collect(Collectors.toList());
        String uri = bundle.getString(keys.get(0));
        testUrl = address + uri;
        return testUrl;
    }
}
